<template>
  <div style="text-align: center;border-radius: 8px;width: 350px;height: 400px;border: 2px solid var(--el-border-color);">
    <div style="font-size: 25px;margin-top: 30px;font-family: STXINWEI">重置密码</div>
    <div style="margin-top: 20px;margin-left: 10px;margin-right: 10px">
      <el-form :model="form" :rules="rules" @validate="onValidated" ref="formRef">

        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" placeholder="请输入新密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password_repeat">
          <el-input v-model="form.password_repeat" type="password" placeholder="重复新密码">
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input v-model="form.email" type="email" placeholder="请输入需要重置密码的邮箱">
            <template #prefix>
              <el-icon><Message /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row :gutter="10" style="width: 100%">
            <el-col :span="17">
              <el-input v-model="form.code" type="text" placeholder="请输入邮箱验证码">
                <template #prefix>
                  <el-icon><EditPen /></el-icon>
                </template>
              </el-input>
            </el-col>
            <el-col :span="5">
              <el-button type="primary" @click="validateEmail"
                         :disabled="!isEmailValid || coldTime > 0 ">
                {{coldTime > 0 ? coldTime + '秒' : '获取验证码'}}
              </el-button>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <div style="margin-top: 30px">
        <el-button @click="resetPassword" type="warning" style="width: 250px" plain>重置密码</el-button>
      </div>
      <div style="margin-top: 10px">
        <el-link @click="router.push('/')" type="info">回到登录界面</el-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import router from "@/router";
import {reactive,ref} from "vue";
import {ElMessage} from "element-plus";
import {post} from "@/utils";

const form = reactive({
  password: '',
  password_repeat: '',
  email: '',
  code: ''
})

const validatePassword = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== form.password) {
    callback(new Error("两次输入的密码不一致"))
  } else {
    callback()
  }
}

const rules = {
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 16, message: '密码长度在6-16个字符之间', trigger: ['blur','change'] },
  ],
  password_repeat: [
    { validator: validatePassword, trigger: ['blur','change'] }
  ],
  email: [
    { required: true, message: '请输入电子邮件', trigger: 'blur' },
    { type: 'email', message: '请输入合法的电子邮件地址', trigger: 'blur' },
  ],
  code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
  ]
}

const formRef = ref()
const isEmailValid = ref(false)
const coldTime = ref(0)

const onValidated = (prop,isValid) => {
  if (prop === 'email'){
    isEmailValid.value = isValid
  }
}

const resetPassword = () =>{
  formRef.value.validate((isValid) => {
    if (isValid){
      post('api/auth//reset-password',{
        password: form.password,
        email: form.email,
        code: form.code
      },(message)  => {
        ElMessage.success(message)
      },(message) => {
        ElMessage.warning(message);
      })
    }else {
      ElMessage.warning('请完整填写表单内容')
    }
  })
}

const validateEmail = () => {
  coldTime.value = 60
  post('/api/auth/valid-reset-email',{
    email: form.email
  }, (message) => {
    ElMessage.success(message)
    setInterval(() => coldTime.value--,1000)
  }, (message) => {
    coldTime.value = 0
    ElMessage.warning(message)
  })
}

</script>

<style scoped>

</style>